package com.emapp.lccode.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.emapp.lccode.R;
import com.emapp.lccode.adapter.PopDistrictAdapter;
import com.emapp.lccode.base.BaseVBRecycleAdapter;
import com.emapp.lccode.base.RecycleUtils;
import com.emapp.lccode.bean.District;
import com.emapp.lccode.bean.SelectDistrict;
import com.emapp.lccode.databinding.PopDistrictBinding;
import com.emapp.lccode.utils.BaseUtil;
import com.hjq.toast.Toaster;

import java.util.ArrayList;

/**
 * Author:YanshanZhang
 * E-mail:690810892@qq.com
 * Date: 2022/7/9 9:42
 * Desc:添加单位---选择单元/巷
 */
public class SelectDistrictDialog extends DialogFragment {
    PopDistrictBinding binding;
    ArrayList<District> datas0 = new ArrayList<>();
    ArrayList<District> datas1 = new ArrayList<>();
    ArrayList<District> datas2 = new ArrayList<>();
    PopDistrictAdapter adapter0, adapter1, adapter2;
    SelectDistrict selectDistrict;
    String town_id, country_id;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(true);
    }

    public void setDatas0(ArrayList<District> datas0) {
        this.datas0 = datas0;
        datas1.clear();
        datas2.clear();
    }

    public ArrayList<District> getDatas0() {
        return datas0;
    }

    public void setTown_id(String town_id) {
        this.town_id = town_id;
    }

    public void setCountry_id(String country_id) {
        this.country_id = country_id;
    }


    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog == null || dialog.getWindow() == null) {
            return;
        }
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setGravity(Gravity.BOTTOM);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Dialog_NoActionBar);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = PopDistrictBinding.inflate(inflater, container, false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return binding.getRoot();
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().getWindow().setWindowAnimations(R.style.dialog_style);
        selectDistrict = new SelectDistrict();
        adapter0 = new PopDistrictAdapter(getActivity(), datas0, 0);
        adapter0.setShowEmptyView(false);
        RecycleUtils.initVerticalRecyle(binding.rvList0);
        binding.rvList0.setAdapter(adapter0);
//        if (datas0.size() > 0) {
//            datas0.get(0).setCheck(true);
//            datas1.clear();
//            datas1.addAll(datas0.get(0).getChildren());
//            selectDistrict.setId_0(datas0.get(0).getValue());
//            selectDistrict.setName_0(datas0.get(0).getPickerViewText());
//            selectDistrict.setId_1("");
//            selectDistrict.setName_1("");
//            selectDistrict.setId_2("");
//            selectDistrict.setName_2("");
//        }
        adapter0.setOnItemClickListener(new BaseVBRecycleAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                if (datas0.get(position).isCheck()) {
                    return;
                }
                for (District d : datas0) {
                    d.setCheck(false);
                }
                datas0.get(position).setCheck(true);
                adapter0.notifyDataSetChanged();
                selectDistrict.setId_0(datas0.get(position).getId());
                selectDistrict.setName_0(datas0.get(position).getName());
                selectDistrict.setId_1("");
                selectDistrict.setName_1("");
                selectDistrict.setId_2("");
                selectDistrict.setName_2("");
                datas1.clear();
                if (datas0.get(position).getChildren() != null) {
                    datas1.addAll(datas0.get(position).getChildren());
                    for (District d : datas1) {
                        d.setCheck(false);
                    }
                }
                adapter1.notifyDataSetChanged();
                datas2.clear();
                adapter2.notifyDataSetChanged();
            }
        });
        adapter1 = new PopDistrictAdapter(getActivity(), datas1, 1);
        adapter1.setShowEmptyView(false);
        RecycleUtils.initVerticalRecyle(binding.rvList1);
        binding.rvList1.setAdapter(adapter1);
        adapter1.setOnItemClickListener(new BaseVBRecycleAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                if (datas1.get(position).isCheck()) {
                    return;
                }
                for (District d : datas1) {
                    d.setCheck(false);
                }
                datas1.get(position).setCheck(true);
                adapter1.notifyDataSetChanged();
                selectDistrict.setId_1(datas1.get(position).getId());
                selectDistrict.setName_1(datas1.get(position).getName());
                selectDistrict.setId_2("");
                selectDistrict.setName_2("");
                datas2.clear();
                if (datas1.get(position).getChildren() != null) {
                    datas2.addAll(datas1.get(position).getChildren());
                    for (District d : datas2) {
                        d.setCheck(false);
                    }
                }
                adapter2.notifyDataSetChanged();
            }
        });
        adapter2 = new PopDistrictAdapter(getActivity(), datas2, 2);
        adapter2.setShowEmptyView(false);
        RecycleUtils.initVerticalRecyle(binding.rvList2);
        binding.rvList2.setAdapter(adapter2);
        adapter2.setOnItemClickListener(new BaseVBRecycleAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                if (datas2.get(position).isCheck()) {
                    return;
                }
                for (District d : datas2) {
                    d.setCheck(false);
                }
                datas2.get(position).setCheck(true);
                adapter2.notifyDataSetChanged();
                selectDistrict.setId_2(datas2.get(position).getId());
                selectDistrict.setName_2(datas2.get(position).getName());
            }
        });
        binding.ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        binding.tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (BaseUtil.isNull(selectDistrict.getId_0())) {
                    Toaster.show("请选择地址");
                    return;
                }
                if (BaseUtil.isNull(selectDistrict.getId_1())) {
                    Toaster.show("请选择地址");
                    return;
                }
                if (BaseUtil.isNull(selectDistrict.getId_2())) {
                    Toaster.show("请选择地址");
                    return;
                }
                SelectDistrictListener listener = (SelectDistrictListener) getActivity();
                if (listener == null) {
                    return;
                }
                listener.onSelectDistrictComplete(selectDistrict);
                dismiss();
            }
        });
    }


    public interface SelectDistrictListener {
        void onSelectDistrictComplete(SelectDistrict district);
    }

}
